package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.*;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.constant.OldmanAppConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.app.DmcPersonMainDto;
import com.csun.cmny.provider.model.dto.button.BindCsunButtonLocationDto;
import com.csun.cmny.provider.model.dto.csunsleep.BindCsunSleepDto;
import com.csun.cmny.provider.model.dto.rfid.BindDmcRfidLabelOldmanDto;
import com.csun.cmny.provider.model.dto.sleepcare.BindSleepCareDto;
import com.csun.cmny.provider.model.dto.watch.BindDmcWatchOldmanDto;
import com.csun.cmny.provider.model.enums.button.DmcCsunButtonBindTypeEnum;
import com.csun.cmny.provider.model.enums.csunsleep.DmcCsunSleepBindTypeEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.enums.sleepcare.DmcSleepCareBindTypeEnum;
import com.csun.cmny.provider.model.vo.device.DmcDeviceVo;
import com.csun.cmny.provider.model.vo.app.AppDeviceTypeVo;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author chenzheng
 * @description:家属版老人APP用户与设备绑定关系服务层
 * @date 2019/9/25 16:24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcDeviceUserBindServiceImpl extends BaseService<DmcDeviceUserBind> implements DmcDeviceUserBindService {
    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcDeviceUserBindMapper dmcDeviceUserBindMapper;

    @Resource
    private DmcWatchOldmanService dmcWatchOldmanService;

    @Resource
    private DmcWatchOldmanMapper dmcWatchOldmanMapper;

    @Resource
    private DmcSleepCareBindMapper dmcSleepCareBindMapper;

    @Resource
    private DmcSleepCareBindService dmcSleepCareBindService;

    @Resource
    private DmcCsunButtonLocationMapper dmcCsunButtonLocationMapper;

    @Resource
    private DmcCsunButtonLocationService dmcCsunButtonLocationService;

    @Resource
    private DmcRfidLabelOldmanMapper dmcRfidLabelOldmanMapper;

    @Resource
    private DmcRfidLabelOldmanService dmcRfidLabelOldmanService;

    @Resource
    private DmcCsunSleepBindService dmcCsunSleepBindService;

    @Resource
    private DmcOldUserBindMapper dmcOldUserBindMapper;

    @Resource
    private OmcRpcService omcRpcService;

    @Resource
    private DmcPropertyManagementService dmcPropertyManagementService;

    @Resource
    private DmcCsunGatewayBindService gatewayBindService;

    @Resource
    private DmcCsunSleepThresholdService csunSleepThresholdService;

    @Resource
    private DmcLifeTransducerDefaultConfigService lifeTransducerDefaultConfigService;

    @Resource
    private DmcYingShiCameraService dmcYingShiCameraService;


    /**
     * 用户通过设备号添加设备
     *
     * @param personMainDto
     * @return
     */
    @Override
    public Integer userBindsDevice(DmcPersonMainDto personMainDto) {

        //校验设备是否存在
        DmcDevice dmcDevice = checkDeviceExsitByDeviceNo(personMainDto.getDeviceNo());
        dmcDevice.setGroupId(1000L);
        /*
        * 2020-07-31
        * xcr
        * 个人设备绑定的机构编号校验
        * */
        int i = dmcDeviceService.update(dmcDevice);
        DmcPropertyManagement dmcPropertyManagement = dmcPropertyManagementService.selectByKey(dmcDevice.getId());
        if(!dmcPropertyManagement.getBuyGroupId().equals(1000L)){
            throw new DmcBizException(ErrorCodeEnum.DMC10067018);
        }
        //更改物管信息
        dmcPropertyManagement.setShipmentDate(new Date());
        dmcPropertyManagement.setInitializeDate(new Date());
        int k = dmcPropertyManagementService.update(dmcPropertyManagement);

        //校验设备是否已经绑定了老人
        confirmDeviceBind(dmcDevice,personMainDto);

        //获取设备ID
        Long deviceId = dmcDevice.getId();

        // TODO 床垫绑定个人需要校验床垫是否为运营方所有

        //校验设备是否已绑定用户
        checkDeviceHasBound(deviceId);

        //进行用户与设备绑定
        DmcDeviceUserBind dmcDeviceUserBind = new DmcDeviceUserBind();
        dmcDeviceUserBind.setDeviceId(deviceId);
        dmcDeviceUserBind.setMaxHeartbeatThreshold(OldmanAppConstant.ORIGINAL_MAX_HEARTBEAT_THRESHOLD);
        dmcDeviceUserBind.setMinHeartbeatThreshold(OldmanAppConstant.ORIGINAL_MIN_HEARTBEAT_THRESHOLD);
        dmcDeviceUserBind.setActionTimesThreshold(OldmanAppConstant.ACTION_TIMES_THRESHOLD);
        dmcDeviceUserBind.setMaxBreathThreshold(OldmanAppConstant.ORIGINAL_MAX_BREATH_THRESHOLD);
        dmcDeviceUserBind.setMinBreathThreshold(OldmanAppConstant.ORIGINAL_MIN_BREATH_THRESHOLD);
        dmcDeviceUserBind.setUserId(personMainDto.getUserId());
        return save(dmcDeviceUserBind);
    }


    /**
     * 查询用户绑定的设备列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<AppDeviceTypeVo> queryDeviceList(Long userId) {

        //查询用户拥有的设备
        DmcDeviceUserBind dubQuery = new DmcDeviceUserBind();
        dubQuery.setUserId(userId);
        List<AppDeviceTypeVo> appDeviceTypeVoList = new LinkedList<>();
        List<DmcDeviceUserBind> deviceUserBinds = select(dubQuery);

        if(deviceUserBinds.size() > 0){
            //获取用户拥有的设备id集合
            List<Long> deviceIdList = deviceUserBinds.stream().map(d -> d.getDeviceId()).collect(Collectors.toList());
            List<DmcDeviceVo> vos = dmcDeviceService.queryDeviceListByIdList(deviceIdList);
            Set<Integer> deviceTypeSet = vos.stream().map(v -> v.getDeviceType()).collect(Collectors.toSet());

            for(Integer type : deviceTypeSet){
                AppDeviceTypeVo typeVo = new AppDeviceTypeVo();
                typeVo.setDeviceType(type);
                List<DmcDeviceVo> tempVoList = new LinkedList<>();
                for(DmcDeviceVo deviceVo : vos){
                    if(typeVo.getDeviceType().equals(deviceVo.getDeviceType())){
                        typeVo.setDeviceTypeName(deviceVo.getTypeName());
                        tempVoList.add(deviceVo);
                    }
                }
                typeVo.setDmcDeviceVoList(tempVoList);
                appDeviceTypeVoList.add(typeVo);
            }
        }
        return appDeviceTypeVoList;
    }

    /**
     * 老人与设备号进行绑定
     *
     * @param personMainDto
     * @return
     */
    @Override
    public Integer saveDeviceOldBind(DmcPersonMainDto personMainDto, LoginAuthDto loginAuthDto) {

        //根据设备编号检验设备是否存在
        DmcDevice dmcDevice = dmcDeviceService.selectByKey(personMainDto.getDeviceId());

        //设备ID
        Long deviceId = dmcDevice.getId();

        //老人id
        Long oldmanId = personMainDto.getOldmanId();

//        //查询老人姓名
//        OldmanRpcDto oldmanRpcDto = new OldmanRpcDto();
//        oldmanRpcDto.setHomeId(loginAuthDto.getUserId().intValue());
//        oldmanRpcDto.setOldmanId(oldmanId.intValue());
//        OldmanRpcVo oldmanVo = omcRpcService.getOldmanByOldmanId(oldmanRpcDto);
//        personMainDto.setOldName(oldmanVo.getOldName());

        //校验该设备是否属于当前登录用户
        checkDeviceIsBelongToThisUser(deviceId,loginAuthDto.getUserId());

//        //校验老人是否属于当前登录用户
//        checkOldmanIsBelongToThisUser(oldmanId,loginAuthDto.getUserId());

        //校验老人是否已绑定过该设备
        boolean bo = confirmDeviceBind(dmcDevice, personMainDto);
        if (bo) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067004);
        }
        //进行设备绑定
        return addDeviceBind(dmcDevice, personMainDto, loginAuthDto);
    }

    /**
     * 老人与设备号进行解绑
     * @param personMainDto
     * @return
     */
    @Override
    public Integer removeOldManBind(DmcPersonMainDto personMainDto) {

        //根据设备编号检验设备是否存在
        DmcDevice dmcDevice = dmcDeviceService.selectByKey(personMainDto.getDeviceId());
        if (null == dmcDevice) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067001);
        }

        //设备ID
        Long deviceId = dmcDevice.getId();

        //校验设备是否已绑定用户
        DmcDeviceUserBind dmcDeviceUserBindDto = new DmcDeviceUserBind();
        dmcDeviceUserBindDto.setDeviceId(deviceId);
        dmcDeviceUserBindDto.setUserId(personMainDto.getUserId());
        int count = selectCount(dmcDeviceUserBindDto);
        if (count <= 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067003);
        }


        //如果是床垫设备,要删除阈值设置,同时删掉个人阈值配置
        if (dmcDevice.getDeviceType().equals(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex())){
            DmcCsunSleepThreshold dmcCsunSleepThreshold = new DmcCsunSleepThreshold();
            dmcCsunSleepThreshold.setDeviceId(dmcDevice.getId());
            csunSleepThresholdService.delete(dmcCsunSleepThreshold);

            DmcDeviceUserBind deviceUserBind = new DmcDeviceUserBind();
            deviceUserBind.setDeviceId(deviceId);
            deviceUserBind.setMinHeartbeatThreshold(0);
            deviceUserBind.setMaxHeartbeatThreshold(0);
            deviceUserBind.setMinBreathThreshold(0);
            deviceUserBind.setMaxBreathThreshold(0);
            deviceUserBind.setActionTimesThreshold(0);
            update(deviceUserBind);
        }

        return deleteDeviceBind(dmcDevice);
    }


    /**
     * 删除设备与用户的绑定关系
     * @param personMainDto
     * @return
     */
    @Override
    public Integer removeDeviceUserBind(DmcPersonMainDto personMainDto) {

        int result = 0;
        //根据设备编号检验设备是否存在

        //设备ID
        Long deviceId = personMainDto.getDeviceId();

        //校验设备是否已绑定用户
        DmcDeviceUserBind dmcDeviceUserBindDto = new DmcDeviceUserBind();
        dmcDeviceUserBindDto.setDeviceId(deviceId);
        dmcDeviceUserBindDto.setUserId(personMainDto.getUserId());
        int count = selectCount(dmcDeviceUserBindDto);
        if (count <= 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067003);
        }

        DmcDevice dmcDevice = dmcDeviceService.selectByKey(deviceId);

        //如果是网关设备,要先移除绑定关系
        if (dmcDevice.getDeviceType().equals(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex())){
            DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
            dmcCsunGatewayBind.setGatewayId(deviceId);
            int delete = gatewayBindService.delete(dmcCsunGatewayBind);

        }
        //如果是床垫设备,要删除阈值设置,同时删掉个人阈值配置
        if (dmcDevice.getDeviceType().equals(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex())){
            DmcCsunSleepThreshold dmcCsunSleepThreshold = new DmcCsunSleepThreshold();
            dmcCsunSleepThreshold.setDeviceId(dmcDevice.getId());
            csunSleepThresholdService.delete(dmcCsunSleepThreshold);

        }

       //删除与用户的绑定关系
        result = delete(dmcDeviceUserBindDto);
       //int result = deleteByKey(deviceId);
        //解除设备与老人的绑定关系
        deleteDeviceBind(dmcDevice);
        return result;
    }

    @Override
    public Integer setDeviceThresholdByDeviceId(DmcPersonMainDto dmcPersonMainDto, LoginAuthDto loginAuthDto) {

        //检验设备是否属于该用户
        checkDeviceIsBelongToThisUser(dmcPersonMainDto.getDeviceId(),loginAuthDto.getUserId());
        DmcDeviceUserBind dmcDeviceUserBind = new DmcDeviceUserBind();
        BeanUtils.copyProperties(dmcPersonMainDto,dmcDeviceUserBind);
        dmcDeviceUserBind.setUserId(loginAuthDto.getUserId());
        dmcDeviceUserBindMapper.setDeviceThresholdByDeviceId(dmcDeviceUserBind);

        //如果是床垫,阈值设置同步到对应的阈值表
        DmcDevice dmcDevice = dmcDeviceService.selectByKey(dmcPersonMainDto.getDeviceId());
        if (dmcDevice.getDeviceType().equals(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex())){
            DmcCsunSleepThreshold dmcCsunSleepThreshold = new DmcCsunSleepThreshold();
            BeanUtils.copyProperties(dmcPersonMainDto,dmcCsunSleepThreshold);

            csunSleepThresholdService.saveOrUpdate(dmcCsunSleepThreshold);
            }


        //校验阈值设置
        if(dmcDeviceUserBind.getMaxHeartbeatThreshold() <= dmcDeviceUserBind.getMinHeartbeatThreshold()){
            throw new DmcBizException(ErrorCodeEnum.DMC10067013);
        }
        if(dmcDeviceUserBind.getMaxBreathThreshold() <= dmcDeviceUserBind.getMinBreathThreshold()){
            throw new DmcBizException(ErrorCodeEnum.DMC10067014);
        }
        if(dmcDeviceUserBind.getActionTimesThreshold() < 1){
            throw new DmcBizException(ErrorCodeEnum.DMC10067015);
        }
        return 1;
    }

    @Override
    public DmcDeviceUserBind getDeviceThresholdByDeviceId(DmcPersonMainDto dmcPersonMainDto, LoginAuthDto loginAuthDto) {

        //查询个人设定阈值
        DmcDeviceUserBind deviceUserBindQuery = new DmcDeviceUserBind();
        deviceUserBindQuery.setUserId(loginAuthDto.getUserId());
        deviceUserBindQuery.setDeviceId(dmcPersonMainDto.getDeviceId());
        deviceUserBindQuery = selectOne(deviceUserBindQuery);

        //如果没有设定个人阈值,下发机构阈值
        if (PublicUtil.isEmpty(deviceUserBindQuery)){
            DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig = lifeTransducerDefaultConfigService.selectByKey(loginAuthDto.getGroupId());

            deviceUserBindQuery.setMaxBreathThreshold(dmcLifeTransducerDefaultConfig.getBreathMaxTimes());
            deviceUserBindQuery.setMinBreathThreshold(dmcLifeTransducerDefaultConfig.getBreathMinTimes());
            deviceUserBindQuery.setMaxHeartbeatThreshold(dmcLifeTransducerDefaultConfig.getHeartbeatMaximum());
            deviceUserBindQuery.setMinHeartbeatThreshold(dmcLifeTransducerDefaultConfig.getHeartbeatMinimum());
        }
        return deviceUserBindQuery;
    }

    @Override
    public Integer breakBindDeviceByOldmanId(Long oldmanId) {

        /*
        * app删除老人时，要解绑该老人的所有设备
        * 目前app中只有【守护佳床垫】设备，现阶段只做了床垫的解绑
        * */
        //定义解绑设备的数量
        int result = 0;

        DmcCsunSleepBind dmcCsunSleepBindQuery = new DmcCsunSleepBind();
        dmcCsunSleepBindQuery.setOldmanId(oldmanId.intValue());
        dmcCsunSleepBindQuery = dmcCsunSleepBindService.selectOne(dmcCsunSleepBindQuery);
        if(dmcCsunSleepBindQuery != null){
           result += dmcCsunSleepBindService.breakCsunSleep(dmcCsunSleepBindQuery.getDeviceId());
        }
        return result;
    }

    /**
     * 查询用户所拥有的设备类型
     *
     * @param userId the user id
     *
     * @return the list.
     */
    @Override
    public List<Long> queryDeviceIdList(Long userId) {

        return dmcDeviceUserBindMapper.queryDeviceIdList(userId);
    }

    /**
     * query not bind device id list
     *
     * @param deviceIdList  the device id list
     *
     * @return  the list.
     */
    @Override
    public List<Long> queryNotBindDeviceIdList(List<Long> deviceIdList) {

//        return dmcDeviceUserBindMapper.queryNotBindDeviceIdList(deviceIdList);

        return null;
    }

    /**
     * query all device id list
     *
     * @return the list.
     */
    @Override
    public List<Long> queryAllDeviceIdList() {

        return dmcDeviceUserBindMapper.queryAllDeviceIdList();
    }


    /**
     * 查询设备是否已进行过绑定
     * 设备类型:0-随访仪 1-手环 2-睡眠卫士 3-警报器 4-网关 5-紧急按钮 6-人体感应 7-门禁设备 8-门禁卡 9-自研床垫 10-护工手表 11-报警灯  12-智能摄像头
     *
     * @return
     */
    public boolean confirmDeviceBind(DmcDevice dmcDevice, DmcPersonMainDto personMainDto) {
        Integer deviceType = dmcDevice.getDeviceType();
        switch (deviceType) {
            case 0:
                //随访仪 无法与老人进行绑定
                return false;
            case 1:
                //手环
                Example example = new Example(DmcWatchOldman.class);
                Example.Criteria criteria = example.createCriteria();
                // 3、校验设备Id或老人Id是否已经
                criteria.andEqualTo("deviceId", dmcDevice.getId());
                example.or(example.createCriteria().andEqualTo("oldmanId", personMainDto.getOldmanId()));
                DmcWatchOldman dmcWatchOldman = dmcWatchOldmanMapper.selectOneByExample(example);
                if (PublicUtil.isNotEmpty(dmcWatchOldman)) {
                    throw new DmcBizException(ErrorCodeEnum.DMC100610012);
                }
                return false;
            case 2:
                //睡眠卫视
                // 2.校验睡眠卫士是否已经被绑定
                DmcSleepCareBind dmcSleepCareBindQuery = dmcSleepCareBindMapper
                        .selectByPrimaryKey(dmcDevice.getId());
                if (PublicUtil.isNotEmpty(dmcSleepCareBindQuery)) {
                    throw new DmcBizException(ErrorCodeEnum.DMC10067002);
                }
                return false;
            case 3:
                //报警器 无法与老人进行绑定
                return false;
            case 4:
                //网关 无法与老人进行绑定
                return false;
            case 5:
                //紧急按钮
                //校验紧急按钮是否已经被绑定
                DmcCsunButtonLocation dmcCsunButtonLocationQuery = dmcCsunButtonLocationMapper
                        .selectByPrimaryKey(dmcDevice.getId());
                if (PublicUtil.isNotEmpty(dmcCsunButtonLocationQuery)) {
                    throw new DmcBizException(ErrorCodeEnum.DMC100610031, dmcCsunButtonLocationQuery.getDeviceId(),
                            dmcCsunButtonLocationQuery.getLocation());
                }
                return false;
            case 6:
                //人体感应 无法与老人进行绑定
                return false;
            case 7:
                //门禁卡设备 无法与老人进行绑定
                return false;
            case 8:
                //门禁卡标签
                // 判断设备Id或者老人Id是否绑定过
                Example example1 = new Example(DmcRfidLabelOldman.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("deviceId", dmcDevice.getId());
                example1.or(example1.createCriteria().andEqualTo("oldmanId", personMainDto.getOldmanId()));
                DmcRfidLabelOldman dmcRfidLabelOldman = dmcRfidLabelOldmanMapper.selectOneByExample(example1);
                if (PublicUtil.isNotEmpty(dmcRfidLabelOldman)) {
                    throw new DmcBizException(ErrorCodeEnum.DMC100610011, dmcRfidLabelOldman.getDeviceId(),
                            dmcRfidLabelOldman.getOldmanId(), dmcRfidLabelOldman.getOldmanName());
                }
                return false;
            case 9:
                //日创床垫
                // 2.校验日创床垫是否已经被绑定
                DmcCsunSleepBind dmcCsunSleepBindQuery = dmcCsunSleepBindService
                        .selectByKey(dmcDevice.getId());
                if (PublicUtil.isNotEmpty(dmcCsunSleepBindQuery)) {
                    throw new DmcBizException(ErrorCodeEnum.DMC10067002, dmcCsunSleepBindQuery.getDeviceId(),
                            dmcCsunSleepBindQuery.getLocation());
                }

                return false;
            case 10:
                //护工手表 无法与老人进行绑定
                return false;
            case 11:
                //报警灯 无法与老人进行绑定
                return false;
            case 12:
                //智能摄像头  暂且没做
                return false;
            case 21:
                //萤石摄像头
                DmcDeviceUserBind dmcDeviceUserBind = new DmcDeviceUserBind();
                dmcDeviceUserBind.setUserId(personMainDto.getOldmanId());
                dmcDeviceUserBind.setUserId(personMainDto.getDeviceId());
                List<DmcDeviceUserBind> select = select(dmcDeviceUserBind);
                if(select.size() > 0){
                    throw new DmcBizException(ErrorCodeEnum.DMC10067002);
                }
                return false;
            default:
                break;
        }
        return true;
    }

    /**
     * 用户和老人进行绑定
     * 设备类型:0-随访仪 1-手环 2-睡眠卫士 3-警报器 4-网关 5-紧急按钮 6-人体感应 7-门禁设备 8-门禁卡 9-自研床垫 10-护工手表 11-报警灯  12-智能摄像头
     *
     * @return
     */
    public Integer addDeviceBind(DmcDevice dmcDevice, DmcPersonMainDto personMainDto, LoginAuthDto loginAuthDto) {
        int result = 0;
        Integer deviceType = dmcDevice.getDeviceType();
        switch (deviceType) {
            case 0:
                //随访仪 无法与老人进行绑定
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);

            case 1:
                //手环
                //填充参数
                BindDmcWatchOldmanDto bindDmcWatchOldmanDto = new BindDmcWatchOldmanDto();
                bindDmcWatchOldmanDto.setDeviceId(dmcDevice.getId());
                bindDmcWatchOldmanDto.setOldmanId(personMainDto.getOldmanId().intValue());
                bindDmcWatchOldmanDto.setOldmanName(personMainDto.getOldName());
                //对孝心手环与老人进行绑定
                result = dmcWatchOldmanService.bindDmcWatchOldman(bindDmcWatchOldmanDto);
                break;
            case 2:
                //睡眠卫视
                //填充参数
                BindSleepCareDto bindSleepCareDto = new BindSleepCareDto();
                bindSleepCareDto.setBindType(DmcSleepCareBindTypeEnum.OLDMAN.getKey());//设备绑定类型 1.老人
                bindSleepCareDto.setDeviceId(dmcDevice.getId());
                bindSleepCareDto.setOldmanId(personMainDto.getOldmanId().intValue());
                bindSleepCareDto.setOldmanName(personMainDto.getOldName());
                // 对睡眠卫士与老人进行绑定
                result = dmcSleepCareBindService.bindSleepCare(bindSleepCareDto, loginAuthDto);
                break;
            case 3:
                //报警器 无法与老人进行绑定
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);

            case 4:
                //网关 无法与老人进行绑定
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);

            case 5:
                //紧急按钮
                //填充参数
                BindCsunButtonLocationDto bindCsunButtonLocationDto = new BindCsunButtonLocationDto();
                bindCsunButtonLocationDto.setBindType(DmcCsunButtonBindTypeEnum.OLDMAN.getKey());
                bindCsunButtonLocationDto.setDeviceId(dmcDevice.getId());
                bindCsunButtonLocationDto.setOldmanId(personMainDto.getOldmanId().intValue());
                bindCsunButtonLocationDto.setOldmanName(personMainDto.getOldName());
                //对紧急按钮与老人进行绑定
                result = dmcCsunButtonLocationService.bindCsunButtonLocation(bindCsunButtonLocationDto, loginAuthDto);
                break;
            case 6:
                //人体感应 无法与老人进行绑定
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);

            case 7:
                //门禁卡设备 无法与老人进行绑定
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);

            case 8:
                //门禁卡标签
                //填充参数
                BindDmcRfidLabelOldmanDto bindDmcRfidLabelOldmanDto = new BindDmcRfidLabelOldmanDto();
                bindDmcRfidLabelOldmanDto.setDeviceId(dmcDevice.getId());
                bindDmcRfidLabelOldmanDto.setOldmanId(personMainDto.getOldmanId());
                bindDmcRfidLabelOldmanDto.setOldmanName(personMainDto.getOldName());
                //对门禁卡标签与老人进行绑定
                result = dmcRfidLabelOldmanService.bindDmcRfidLabelOldman(bindDmcRfidLabelOldmanDto);
                break;
            case 9:
                //日创床垫
                //填充参数
                BindCsunSleepDto bindCsunSleepDto = new BindCsunSleepDto();
                bindCsunSleepDto.setBindType(DmcCsunSleepBindTypeEnum.OLDMAN.getKey());
                bindCsunSleepDto.setDeviceId(dmcDevice.getId());
                bindCsunSleepDto.setOldmanId(personMainDto.getOldmanId().intValue());
                bindCsunSleepDto.setOldmanName(personMainDto.getOldName());
                //对日创床垫与老人进行绑定
                result = dmcCsunSleepBindService.bindCsunSleep(bindCsunSleepDto, loginAuthDto);
                break;
            case 10:
                //护工手表 无法与老人进行绑定
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);

            case 11:
                //报警灯 无法与老人进行绑定
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);

            case 12:
                //智能摄像头  暂且没做
                throw new DmcBizException(ErrorCodeEnum.DMC10067006);
            case 21:
                //萤石摄像头
                DmcDeviceUserBind dmcDeviceUserBind = new DmcDeviceUserBind();
                dmcDeviceUserBind.setUserId(personMainDto.getOldmanId());
                dmcDeviceUserBind.setDeviceId(personMainDto.getDeviceId());
                save(dmcDeviceUserBind);
                break;
            default:
                return result;

        }
        return result;
    }


    /**
     * 老人和设备进行解绑
     * 设备类型:0-随访仪 1-手环 2-睡眠卫士 3-警报器 4-网关 5-紧急按钮 6-人体感应 7-门禁设备 8-门禁卡 9-自研床垫 10-护工手表 11-报警灯  12-智能摄像头
     *
     * @return
     */
    public Integer deleteDeviceBind(DmcDevice dmcDevice) {
        int result = 0;
        Long dviceId = dmcDevice.getId();
        Integer deviceType = dmcDevice.getDeviceType();
        switch (deviceType) {
            case 0:
                //随访仪 无法与老人进行解绑
                /**
                 * @data    2020-04-13
                 * @author  chisj
                 * @desc    随访仪不需要和老人解绑
                 */
                // throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            case 1:
                //手环
                //对孝心手环与老人进行解绑
                result = dmcWatchOldmanService.breakDmcWatchOldmanByDeviceId(dviceId);
                break;
            case 2:
                //睡眠卫视
                // 对睡眠卫士与老人进行解绑
                result = dmcSleepCareBindService.breakSleepCare(dviceId);
                break;
            case 3:
                //报警器 无法与老人进行解绑
                /**
                 * @data    2020-04-13
                 * @author  chisj
                 * @desc    报警器不需要和老人解绑
                 */
                //throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            case 4:
                //网关 无法与老人进行解绑
                /**
                 * @data    2020-04-13
                 * @author  chisj
                 * @desc    网关不需要和老人解绑
                 */
                // throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            case 5:
                //紧急按钮
                //对紧急按钮与老人进行解绑
                result = dmcCsunButtonLocationService.breakCsunButtonLocation(dviceId);
                break;
            case 6:
                //人体感应 无法与老人进行解绑
                /**
                 * @data    2020-04-13
                 * @author  chisj
                 * @desc    人体感应不需要和老人解绑
                 */
                // throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            case 7:
                //门禁卡设备 无法与老人进行解绑
                /**
                 * @data    2020-04-13
                 * @author  chisj
                 * @desc    门禁卡设备不需要和老人解绑
                 */
                // throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            case 8:
                //门禁卡标签
                //对门禁卡标签与老人进行解绑
                result = dmcRfidLabelOldmanService.breakDmcRfidLabelOldmanByDeviceId(dviceId);
                break;
            case 9:
                //日创床垫
                //对日创床垫与老人进行解绑
                DmcCsunSleepBind dmcCsunSleepBind = dmcCsunSleepBindService.selectByKey(dviceId);
                if(dmcCsunSleepBind != null){
                    result = dmcCsunSleepBindService.breakCsunSleep(dviceId);
                }
                break;
            case 10:
                //护工手表 无法与老人进行解绑
                /**
                 * @data    2020-04-13
                 * @author  chisj
                 * @desc    护工手表设备不需要和老人解绑
                 */
                // throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            case 11:
                //报警灯 无法与老人进行解绑
                /**
                 * @data    2020-04-13
                 * @author  chisj
                 * @desc    报警灯设备不需要和老人解绑
                 */
                //throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            case 12:
                //智能摄像头  暂且没做
                //throw new DmcBizException(ErrorCodeEnum.DMC10067007);
                break;
            default:
                return result;
        }
        return result;

    }

    //校验设备是否已经绑定
    public void checkDeviceHasBound(Long deviceId){
        DmcDeviceUserBind dmcDeviceUserBindDto = new DmcDeviceUserBind();
        dmcDeviceUserBindDto.setDeviceId(deviceId);
        int count = selectCount(dmcDeviceUserBindDto);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067002);
        }
    }

    //根据设备编号检验设备是否存在
    public DmcDevice checkDeviceExsitByDeviceNo(String deviceNo){

        DmcDevice dmcDevice = dmcDeviceService.queryDeviceByNumber(deviceNo);
        if (null == dmcDevice) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067001);
        }
        return dmcDevice;
    }

    //校验设备是否属于当前用户
    public void checkDeviceIsBelongToThisUser(Long deviceId,Long userId){

        DmcDeviceUserBind dmcDeviceUserBindDto = new DmcDeviceUserBind();
        dmcDeviceUserBindDto.setDeviceId(deviceId);
        dmcDeviceUserBindDto = selectOne(dmcDeviceUserBindDto);

        if (dmcDeviceUserBindDto == null) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067003);
        }else{
            if(!dmcDeviceUserBindDto.getUserId().equals(userId)){
                throw new DmcBizException(ErrorCodeEnum.DMC10067008);
            }
        }
    }

    //校验老人是否属于当前用户
    public void checkOldmanIsBelongToThisUser(Long oldmanId,Long userId){

        DmcOldUserBind oldUserBindDto = new DmcOldUserBind();
        oldUserBindDto.setOldManId(oldmanId);
        oldUserBindDto = dmcOldUserBindMapper.selectOne(oldUserBindDto);

        if (oldUserBindDto == null) {
            throw new DmcBizException(ErrorCodeEnum.DMC10067005);
        }else{
            if(!oldUserBindDto.getUserId().equals(userId)){
//                throw new DmcBizException(ErrorCodeEnum.DMC10067009);
            }
        }
    }
}
